package com.havilahshawn.finaltest22ndsesforad.DB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import static com.havilahshawn.finaltest22ndsesforad.DB.StuContract.StuEntry.COLUMN_STU_AGE;
import static com.havilahshawn.finaltest22ndsesforad.DB.StuContract.StuEntry.COLUMN_STU_NAME;
import static com.havilahshawn.finaltest22ndsesforad.DB.StuContract.StuEntry.COLUMN_STU_SEX;

/**
 * Created by My Family on 2018/6/28.
 */

public class StudentDbOpenHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "Students.db";
    private static final int DATABASE_VERSION = 1;
    private static final String SQL_CREATE_STUDENT = "CREATE TABLE " + StuContract.StuEntry.TABLE_NAME  +
            "(" + StuContract.StuEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"+
            COLUMN_STU_NAME + " TEXT NOT NULL,"+
            COLUMN_STU_AGE + " INTEGER NOT NULL," +
            COLUMN_STU_SEX + " INTEGER NOT NULL" + ");";
    private final Context mContext;
    public StudentDbOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mContext = context;

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(SQL_CREATE_STUDENT);
        sqLiteDatabase.execSQL("INSERT INTO " + StuContract.StuEntry.TABLE_NAME + " ("
                + COLUMN_STU_NAME + ","
                + COLUMN_STU_AGE + ","
                + COLUMN_STU_SEX+ ")"+
                "VALUES('South Korea','18', '0')"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        mContext.deleteDatabase(DATABASE_NAME);
    }
}
